package com.zr.planclass.controller;

import com.zr.enumvo.CurrencyCodeEnum;
import com.zr.enumvo.IsTaxEnum;
import com.zr.enumvo.StatusEnum;
import com.zr.enumvo.TypeEnum;
import com.zr.planclass.model.*;
import com.zr.planclass.service.PlanClassService;
import com.zr.planclass.util.ResultBuilderVo;
import com.zr.planclass.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lenovo on 2019/12/22.
 */
@RestController
@CrossOrigin//支持跨越
public class PlanClassController {
    @Autowired
    private PlanClassService planClassService;

    /*新增计划大类*/
    @PostMapping("planCategory/add222222222")
    public ResultVo add(@RequestBody @Valid PlanClassAddVo planClassAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        List<PlanClassAddVo> planClassAddVoList = new ArrayList<>();
        planClassAddVoList.add(planClassAddVo);
        ResultVo resultVo = planClassService.addPlanClass(planClassAddVoList);
        return resultVo;
    }
    /*修改计划大类*/
    @PostMapping("planCategory/update")
    public ResultVo update(@RequestBody @Valid PlanClassUpdateVo planClassUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return planClassService.updatePlanClass(planClassUpdateVo);
    }
    /*修改计划大类启用禁用状态*/
    @PostMapping("planCategory/updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid PlanClassUpdateStatusVo planClassUpdateStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return planClassService.updateStatus(planClassUpdateStatusVo);
    }
    /*查看*/
    @GetMapping("/planClass/see")
    public ResultVo see(Integer id){
        if (id==null){
            return ResultBuilderVo.error("查看时id不能为空");
        }
        return planClassService.see(id);
    }

    /*分页*/
    @PostMapping("planCategory/queryPage")
    public ResultVo queryPage(@RequestBody PlanClassSelectVo planClassSelectVo){

        return planClassService.queryPage(planClassSelectVo);
    }
    @PostMapping("metadata/getPullDownList")
    public ResultVo getPullDownList(){
        Map<String,List> map = new HashMap<String,List>();
//询价状态
        List inquiryStatusList = new ArrayList();
        for (StatusEnum c:StatusEnum.values()) {
            StatusVo statusVo1 = new StatusVo();
            statusVo1.setLabel(c.getName());
            statusVo1.setValue(c.getValue());
            inquiryStatusList.add(statusVo1);
        }
        map.put("inquiryStatus",inquiryStatusList);
//询价类型
        List inquiryTypeList = new ArrayList();
        for (TypeEnum c:TypeEnum.values()) {
            StatusVo statusVo2 = new StatusVo();
            statusVo2.setLabel(c.getName());
            statusVo2.setValue(c.getIndex());
            inquiryTypeList.add(statusVo2);
        }
        map.put("type",inquiryTypeList);
//询价币别
        List inquiryCurrencyList = new ArrayList();
        for (CurrencyCodeEnum c:CurrencyCodeEnum.values()) {
            StatusVo statusVo4 = new StatusVo();
            statusVo4.setLabel(c.getName());
            statusVo4.setValue(c.getIndex());
            inquiryCurrencyList.add(statusVo4);
        }
        map.put("currencyCode",inquiryCurrencyList);
//是否含税
        List isIncludeTaxList = new ArrayList();
        for (IsTaxEnum c:IsTaxEnum.values()) {
            StatusVo statusVo3 = new StatusVo();
            statusVo3.setLabel(c.getName());
            statusVo3.setValue(c.getIndex());
            isIncludeTaxList.add(statusVo3);
        }
        map.put("isTax",isIncludeTaxList);
//计划大类状态
        List status = new ArrayList();
        for (StatusEnum c:StatusEnum.values()) {
            StatusVo statusVo5 = new StatusVo();
            statusVo5.setLabel(c.getName());
            statusVo5.setValue(c.getValue());
            status.add(statusVo5);
        }
        map.put("status",status);

        return ResultBuilderVo.success(map);

    }

    @PostMapping("quotation/InquiryIntroduction")
    public ResultVo importPlanClass(MultipartFile file)throws Exception{
        return planClassService.importPlanClass(file);
    }

   /* @GetMapping("export")
    public ResultVo export(HttpServletResponse response, String code, String name, Integer status)throws Exception{
        return planClassService.export(response,code,name,status);
    }*/


}
